/*
 * DBConnection.java
 *
 * Created on 22 November 2007, 13:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.mitac.mcd.cs.mobiletracking.directoryserver.util;


import java.beans.PropertyVetoException;
import java.sql.*;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mitac.mcd.cs.mobiletracking.directoryserver.MobileServer;


public class DBConnection {

	private static ComboPooledDataSource aut_ds;
	private static ComboPooledDataSource gpsdata_ds;
    
    public static synchronized Connection getConnection(String type) throws SQLException, PropertyVetoException{
    	java.sql.Connection db = null;
    	if (type.equalsIgnoreCase("aut")){
    		if(aut_ds==null) initAutConnection();
    		db = aut_ds.getConnection();
    	}else if (type.equalsIgnoreCase("gpsdata")){
    		if(gpsdata_ds==null) initGPSDATAConnection();
    		db = gpsdata_ds.getConnection();
    	}
    	return db;
    }
    
    public static void initGPSDATAConnection() throws PropertyVetoException, SQLException{
    	//设置DataSource
    	gpsdata_ds = new ComboPooledDataSource();
    	
    	String username = MobileServer.ConfigProperties.get("GPSDATA_DB_USERNAME").toString();
        String password = MobileServer.ConfigProperties.get("GPSDATA_DB_PASSWORD").toString();
    	String ip = MobileServer.ConfigProperties.get("GPSDATA_DB_IP").toString();
        String port = MobileServer.ConfigProperties.get("GPSDATA_DB_PORT").toString();
        String dbname = MobileServer.ConfigProperties.get("GPSDATA_DB_NAME").toString();
    	String type = MobileServer.ConfigProperties.get("GPSDATA_DB_TYPE").toString();
    	String url = null;String driverClassName = null;
    	if (type.equalsIgnoreCase("mysql")){
            driverClassName = "com.mysql.jdbc.Driver";
            url = "jdbc:mysql://"+ip+":"+port+"/"+dbname+"?useUnicode=true&amp;characterEncoding=utf-8";
    	}else if (type.equalsIgnoreCase("db2")){
            driverClassName = "com.ibm.db2.jcc.DB2Driver";
            url = "jdbc:db2://"+ip+":"+port+"/"+dbname+"?useUnicode=true&amp;characterEncoding=utf-8";
    	}
    	gpsdata_ds.setDriverClass(driverClassName);
    	gpsdata_ds.setJdbcUrl(url);
    	gpsdata_ds.setUser(username);
    	gpsdata_ds.setPassword(password);
    	
    	gpsdata_ds.setAcquireIncrement(Integer.parseInt(MobileServer.jdbcProperties.get("acquireIncrement").toString()));
    	gpsdata_ds.setAcquireRetryAttempts(Integer.parseInt(MobileServer.jdbcProperties.get("acquireRetryAttempts").toString()));
    	gpsdata_ds.setAcquireRetryDelay(Integer.parseInt(MobileServer.jdbcProperties.get("acquireRetryDelay").toString()));
    	gpsdata_ds.setAutoCommitOnClose(Boolean.parseBoolean(MobileServer.jdbcProperties.get("autoCommitOnClose").toString()));
    	gpsdata_ds.setAutomaticTestTable(MobileServer.jdbcProperties.get("automaticTestTable").toString());
    	gpsdata_ds.setBreakAfterAcquireFailure(Boolean.parseBoolean(MobileServer.jdbcProperties.get("breakAfterAcquireFailure").toString()));
    	gpsdata_ds.setCheckoutTimeout(Integer.parseInt(MobileServer.jdbcProperties.get("checkoutTimeout").toString()));
    	gpsdata_ds.setIdleConnectionTestPeriod(Integer.parseInt(MobileServer.jdbcProperties.get("idleConnectionTestPeriod").toString()));
    	gpsdata_ds.setInitialPoolSize(Integer.parseInt(MobileServer.jdbcProperties.get("initialPoolSize").toString()));
    	gpsdata_ds.setMaxIdleTime(Integer.parseInt(MobileServer.jdbcProperties.get("maxIdleTime").toString()));
    	gpsdata_ds.setMaxPoolSize(Integer.parseInt(MobileServer.jdbcProperties.get("maxPoolSize").toString()));
    	gpsdata_ds.setMinPoolSize(Integer.parseInt(MobileServer.jdbcProperties.get("minPoolSize").toString()));
    	gpsdata_ds.setMaxStatements(Integer.parseInt(MobileServer.jdbcProperties.get("maxStatements").toString()));
    	gpsdata_ds.setMaxStatementsPerConnection(Integer.parseInt(MobileServer.jdbcProperties.get("maxStatementsPerConnection").toString()));
    	gpsdata_ds.setNumHelperThreads(Integer.parseInt(MobileServer.jdbcProperties.get("numHelperThreads").toString()));
    	gpsdata_ds.setPreferredTestQuery(MobileServer.jdbcProperties.get("preferredTestQuery").toString());
    	gpsdata_ds.setPropertyCycle(Integer.parseInt(MobileServer.jdbcProperties.get("propertyCycle").toString()));
    	gpsdata_ds.setTestConnectionOnCheckout(Boolean.parseBoolean(MobileServer.jdbcProperties.get("testConnectionOnCheckout").toString()));
    	gpsdata_ds.setTestConnectionOnCheckin(Boolean.parseBoolean(MobileServer.jdbcProperties.get("testConnectionOnCheckin").toString()));
    	gpsdata_ds.getConnection();//test connect db,if fail then throw exception
    }
    
    public static void initAutConnection() throws PropertyVetoException, SQLException{
    	//设置DataSource
    	aut_ds = new ComboPooledDataSource();
    	
    	String type = MobileServer.ConfigProperties.get("AUT_DB_TYPE").toString();
    	String username = MobileServer.ConfigProperties.get("AUT_DB_USERNAME").toString();
        String password = MobileServer.ConfigProperties.get("AUT_DB_PASSWORD").toString();
        String ip = MobileServer.ConfigProperties.get("AUT_DB_IP").toString();
        String port = MobileServer.ConfigProperties.get("AUT_DB_PORT").toString();
        String dbname = MobileServer.ConfigProperties.get("AUT_DB_NAME").toString();
    	String url = null;String driverClassName = null;
    	if (type.equalsIgnoreCase("mysql")){
            driverClassName = "com.mysql.jdbc.Driver";
            url = "jdbc:mysql://"+ip+":"+port+"/"+dbname+"?useUnicode=true&amp;characterEncoding=utf-8";
    	}else if (type.equalsIgnoreCase("db2")){
            driverClassName = "com.ibm.db2.jcc.DB2Driver";
            url = "jdbc:db2://"+ip+":"+port+"/"+dbname+"?useUnicode=true&amp;characterEncoding=utf-8";
    	}
    	aut_ds.setDriverClass(driverClassName);
    	aut_ds.setJdbcUrl(url);
    	aut_ds.setUser(username);
    	aut_ds.setPassword(password);
    	
    	aut_ds.setAcquireIncrement(Integer.parseInt(MobileServer.jdbcProperties.get("acquireIncrement").toString()));
    	aut_ds.setAcquireRetryAttempts(Integer.parseInt(MobileServer.jdbcProperties.get("acquireRetryAttempts").toString()));
    	aut_ds.setAcquireRetryDelay(Integer.parseInt(MobileServer.jdbcProperties.get("acquireRetryDelay").toString()));
    	aut_ds.setAutoCommitOnClose(Boolean.parseBoolean(MobileServer.jdbcProperties.get("autoCommitOnClose").toString()));
    	aut_ds.setAutomaticTestTable(MobileServer.jdbcProperties.get("automaticTestTable").toString());
    	aut_ds.setBreakAfterAcquireFailure(Boolean.parseBoolean(MobileServer.jdbcProperties.get("breakAfterAcquireFailure").toString()));
    	aut_ds.setCheckoutTimeout(Integer.parseInt(MobileServer.jdbcProperties.get("checkoutTimeout").toString()));
    	aut_ds.setIdleConnectionTestPeriod(Integer.parseInt(MobileServer.jdbcProperties.get("idleConnectionTestPeriod").toString()));
    	aut_ds.setInitialPoolSize(Integer.parseInt(MobileServer.jdbcProperties.get("initialPoolSize").toString()));
    	aut_ds.setMaxIdleTime(Integer.parseInt(MobileServer.jdbcProperties.get("maxIdleTime").toString()));
    	aut_ds.setMaxPoolSize(Integer.parseInt(MobileServer.jdbcProperties.get("maxPoolSize").toString()));
    	aut_ds.setMinPoolSize(Integer.parseInt(MobileServer.jdbcProperties.get("minPoolSize").toString()));
    	aut_ds.setMaxStatements(Integer.parseInt(MobileServer.jdbcProperties.get("maxStatements").toString()));
    	aut_ds.setMaxStatementsPerConnection(Integer.parseInt(MobileServer.jdbcProperties.get("maxStatementsPerConnection").toString()));
    	aut_ds.setNumHelperThreads(Integer.parseInt(MobileServer.jdbcProperties.get("numHelperThreads").toString()));
    	aut_ds.setPreferredTestQuery(MobileServer.jdbcProperties.get("preferredTestQuery").toString());
    	aut_ds.setPropertyCycle(Integer.parseInt(MobileServer.jdbcProperties.get("propertyCycle").toString()));
    	aut_ds.setTestConnectionOnCheckout(Boolean.parseBoolean(MobileServer.jdbcProperties.get("testConnectionOnCheckout").toString()));
    	aut_ds.setTestConnectionOnCheckin(Boolean.parseBoolean(MobileServer.jdbcProperties.get("testConnectionOnCheckin").toString()));
    	aut_ds.getConnection();//test connect db,if fail then throw exception
    }
    
    public void close() {
    	
    }
    
}
